

#include <assert.h>
#include <string.h>
#include <stdlib.h>


#include "vector_fd.h"




VectorFD *create_vector_fd(int count)
{
    VectorFD *vfd = (VectorFD *)malloc(sizeof(VectorFD));
    assert(vfd != NULL);

    vfd->fd = (int *)malloc(sizeof(int) * count);
    assert(vfd != NULL);
    
    vfd->counter = 0;
    vfd->max_counter = count;

    return vfd;
}

void destroy_verctor_fd(VectorFD *vfd)
{
    assert(vfd != NULL);
    free(vfd->fd);
    free(vfd);
}

int get_fd(VectorFD *vfd, int index)
{
  
    assert(vfd != NULL);
    assert(index < vfd->max_counter);
    
    int fd = vfd->fd[index];
   
    return fd;
}

void remove_fd(VectorFD *vfd, int fd)
{
 
    assert(vfd != NULL);

    int i = 0;
    for (; i < vfd->counter; ++i)
    {
        if (vfd->fd[i] == fd)
        {
            vfd->fd[i] = -1;
            vfd->counter--;
            break;
        }
    }
  
}

void add_fd(VectorFD *vfd, int fd)
{
  
    assert(vfd != NULL);

    if (vfd->counter >= vfd->max_counter)
    {
        int size = vfd->max_counter * 2;
        int *ptr = (int *)malloc(sizeof(int) * size);
        assert(ptr != NULL);
        free(vfd->fd);

        vfd->fd = ptr;
        vfd->max_counter = size;
    }

    vfd->fd[vfd->counter++] = fd;
   
}
